using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using UnityEngine;

public class lesson3 : IXmlSerializable
{
    public int age;
    public string name;
    public XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(XmlReader reader)
    {
        //this.age = int.Parse(reader["age"]);
        //this.name = reader["name"];
        //while(reader.Read())
        //{
        //    if(reader.NodeType == XmlNodeType.Element)
        //    {
        //        switch (reader.Name) {

        //            case "age":
        //                reader.Read();
        //                this.age = int.Parse(reader.Value);
        //                break;
        //                case "name":
        //                reader.Read();
        //                this.name = reader.Value;
        //                break;
        //        }


        //    }
        //}
        XmlSerializer s1=new XmlSerializer(typeof(int));
        XmlSerializer s2 = new XmlSerializer(typeof(string));
        reader.Read();
        reader.ReadStartElement("age");
        this.age = (int)s1.Deserialize(reader);
        reader.ReadEndElement();
        reader.ReadStartElement("name");
        this.name = s2.Deserialize(reader).ToString();
        reader.ReadEndElement();
        Debug.Log(age + name);
    }

    public void WriteXml(XmlWriter writer)
    {
        //writer.WriteAttributeString("age", this.age.ToString());
        //writer.WriteAttributeString("name", name);
        //writer.WriteElementString("age", this.age.ToString());

        //writer.WriteElementString("name", name);
        XmlSerializer s1 = new XmlSerializer(typeof(int));
        writer.WriteStartElement("age");
        s1.Serialize(writer, age);
        writer.WriteEndElement();
        XmlSerializer s2=new XmlSerializer(typeof(string));
        writer.WriteStartElement("name");
        s2.Serialize(writer, name);
        writer.WriteEndElement();
    }
}

public class Lesson3 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        lesson3 l=new lesson3();
        l.name = "asd";
        string path=Application.persistentDataPath + "/Lesson3Test.xml";
        Debug.Log(path);
        

            using (StreamWriter xmlreader = new StreamWriter(path))
            {
                XmlSerializer s=new XmlSerializer(typeof(lesson3));
                s.Serialize(xmlreader,l);
            }
         
        

            using (StreamReader xmlreader = new StreamReader(path))
            {
                XmlSerializer s = new XmlSerializer(typeof(lesson3));
                lesson3 a= s.Deserialize(xmlreader) as lesson3;
            }
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
